//
// Created by francklinson on 2021/5/9.
//

#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

class Solution {
public:
    string simplifyPath(string path) {
        if (path.size() == 1) return path;
        stack<string> pathStack{};
        int i = 0;
        string seg{};
        while (i < path.size()) {
            if (path[i] == '/') {
                while (path[i] == '/') {
                    ++i;
                }
                continue;
            }
            if (path[i] == '.') {
                while (path[i] == '.') {
                    seg.push_back('.');
                    ++i;
                }
                if (seg.size() == 2) {
                    if (!pathStack.empty()) pathStack.pop();
                }
                if (seg.size() > 2) {
                    pathStack.push(seg);
                }
                seg = "";
                continue;
            } else {
                while (path[i] != '.' && path[i] != '/' && i<path.size()) {
                    seg.push_back(path[i]);
                    ++i;
                }
                pathStack.push(seg);
                seg = "";
            }
        }
        string ans{};
        while (!pathStack.empty()) {
            ans = "/" + pathStack.top() + ans;

            pathStack.pop();
        }

        return ans.empty() ? "/" : ans;

    }
};

class Solution2 {
public:
    string simplifyPath(string path) {
        stringstream ss(path);
        vector<string> strs;
        strs.reserve(20);
        string curr;
        while (getline(ss, curr, '/'))
        {
            if (curr != "." && !curr.empty())
            {
                if (curr != "..")
                {
                    strs.push_back(curr);
                }
                else if (!strs.empty())
                {
                    strs.pop_back();
                }
            }
        }

        if (!strs.empty())
        {
            string res = "";
            for (string str : strs)
            {
                res.append("/");
                res.append(str);
            }
            return res;
        }
        else
        {
            // 为空，直接返回 "/"
            return "/";
        }
    }
};


class Solution3 { // 优秀解答
public:
    string simplifyPath(string path) {
        vector<string> strVec;
        stack<string> strStack;
        split_string(path, strVec, "/");
        for (auto & i : strVec) {
            if (i.empty() || i == ".") {
                continue;
            }
            if (i == "..") {
                if (!strStack.empty()) {
                    strStack.pop();
                }
                continue;
            }
            strStack.push(i);
        }

        if (strStack.empty()){
            return "/";
        }

        string res = strStack.top();
        strStack.pop();
        while (!strStack.empty()) {
            res = strStack.top() + "/"+res;
            strStack.pop();
        }
        return "/"+res;
    }

private:
    void split_string(const std::string &s, std::vector<std::string> &v, const std::string &c) {
        std::string::size_type pos1, pos2;
        pos2 = s.find(c);
        pos1 = 0;
        while (std::string::npos != pos2) {
            v.push_back(s.substr(pos1, pos2 - pos1));
            pos1 = pos2 + c.size();
            pos2 = s.find(c, pos1);
        }
        if (pos1 != s.length()) {
            v.push_back(s.substr(pos1));
        }
    }
};
int main() {
    Solution2 sol;
    cout << sol.simplifyPath("/home/") << endl;
    cout << sol.simplifyPath("/../") << endl;
    cout << sol.simplifyPath("/home//foo/") << endl;
    cout << sol.simplifyPath("/a/./b/../../c/") << endl;
    cout << sol.simplifyPath("/..hiden") << endl;
    return 0;
}